<template>
  <div>
    <div style="margin-bottom: 50px">
      搜索内容：<el-input v-model="vo.keyWord" placeholder="请输入角色或拼音搜索" style="width: 222px"></el-input>
      &nbsp;&nbsp;
      创建时间：
      <el-date-picker
          v-model="vo.begin"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm"
          placeholder="选择日期时间">
      </el-date-picker>
      ~
      <el-date-picker
          v-model="vo.end"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm"
          placeholder="选择日期时间">
      </el-date-picker>
      &nbsp;&nbsp;
      <el-button type="primary" icon="el-icon-search" circle @click="search(1)"></el-button>
      <el-button type="primary" icon="el-icon-first-aid-kit" circle @click="toAdd"></el-button>
    </div>
    <el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header">
      <el-table-column prop="id" label="ID"></el-table-column>
      <el-table-column prop="name" label="角色"></el-table-column>
      <el-table-column prop="nameZh" label="角色拼音"></el-table-column>
      <el-table-column prop="created" label="创建时间"></el-table-column>
<!--      <el-table-column prop="img" label="头像"></el-table-column>-->
      <el-table-column label="操作" width="180" align="center">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.$index, scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        background
        layout="prev, pager, next"
        @current-change="search"
        :current-page="cp"
        :pageSize="ps"
        :total="total">
    </el-pagination>


    <el-dialog title="编辑" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="角色名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色拼音" :label-width="formLabelWidth">
          <el-input v-model="form.nameZh" autocomplete="off"></el-input>
        </el-form-item>
<!--        <el-form-item label="图标" :label-width="formLabelWidth">-->
<!--          <el-input v-model="form.img" autocomplete="off"></el-input>-->
<!--        </el-form-item>-->
        <el-form-item label="权限树" :label-width="formLabelWidth">
          <!--          <el-input v-model="form.img" autocomplete="off"></el-input>-->

          <el-tree
              :data="tree"
              show-checkbox
              node-key="id"
              ref="tree"
              :default-checked-keys="mids"
              :props="defaultProps">
          </el-tree>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "Role",
  data() {
    return {
      tableData: [],
      vo: {},
      total: 10,
      ps: 2,
      cp: 1,
      dialogFormVisible: false,
      form: {},
      formLabelWidth: '120px',
      tree: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      mids: []
    }
  },
  methods: {
    search(cp = 1) {
      this.cp = cp
      axios.post(this.$baseUrl + '/role/search/' + this.cp + '/' + this.ps, this.vo).then(res => {
        this.tableData = res.data.rows;
        this.total = res.data.total;
      })
    },
    toAdd() {
      this.mids = []
      this.form = {}
      this.dialogFormVisible = true
      this.getTree();

    },
    getTree() {
      axios.get(this.$baseUrl + '/menu/getTree').then(res => {
        this.tree = res.data.data;
      })
    },
    handleEdit(index, row) {
      this.mids = []
      this.dialogFormVisible = true;
      // 获取树结构
      this.getTree();
      // 回显数据
      this.form = row;
      // 获取当前角色的权限
      axios.get(this.$baseUrl + '/role/getMidsByRid/' + row.id).then(res => {
        this.mids = res.data.data;

      })
    },
    save() {
      // 获取所选菜单ID
      let nodes = this.$refs.tree.getCheckedNodes();
      let ids = [];
      nodes.map(item => {
        if (item.children.length === 0) {
          ids.push(item.id)
        }
      })
      // 整合到Form
      this.form.ids = ids;
      let method = null;
      if (this.form.id != null) {
        // 修改操作
        method = axios.put(this.$baseUrl + '/role', this.form)
      } else {
        // 添加操作
        method = axios.post(this.$baseUrl + '/role', this.form)
      }

      method.then(res => {
        if (res.data.flag) {
          alert('操作成功')
          // 关闭对话框
          this.dialogFormVisible = false
          this.search(1)
        }
      })
    },
    handleDelete(index, row) {
      axios.delete(this.$baseUrl + '/role/' + row.id).then(res => {
        if (res.data.flag) {
          alert('操作成功')
          // 关闭对话框
          this.search(1)
        }
      })
    }

  },
  created() {
    this.search();
  }
}
</script>

<style scoped>

</style>